package myservlet.control;

import mybean.data.Account_Bean;
import mybean.data.shopContent_Bean;

import java.io.*;
import java.sql.*;
import java.text.MessageFormat;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/productDelete")
public class productDelete extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        Account_Bean LoginBean = null;
        //用户信息的bean
        try {
            LoginBean = (Account_Bean) session.getAttribute("LoginBean");
            if (LoginBean == null) {
                LoginBean = new Account_Bean();  //创建Javabean对象
                session.setAttribute("LoginBean", LoginBean);
            }
        } catch (Exception exp) {
            LoginBean = new Account_Bean();
            session.setAttribute("LoginBean", LoginBean);
        }

        //判断当前是否是登录状态
        if (LoginBean.getUsername().equals("") || LoginBean.getPassword().equals("")) {
            fail(request, response, "请您先登录再删除您的商品");
            //没有登录, 不给删除
            return;
        }

        //获取用户名，待会儿要写入数据库
        String username = LoginBean.getUsername();

        shopContent_Bean pageBean = null;
        //获取添加的商品类型
        try {
            pageBean = (shopContent_Bean) session.getAttribute("pageBean");
            if (pageBean == null) {
                pageBean = new shopContent_Bean();  //创建Javabean对象
                session.setAttribute("pageBean", pageBean);
            }
        } catch (Exception exp) {
            pageBean = new shopContent_Bean();
            session.setAttribute("pageBean", pageBean);
        }

        request.setCharacterEncoding("gb2312");
        String dataBase = "web_used";
        String tableName = pageBean.getShopType();
        System.out.println(tableName);

        request.setCharacterEncoding("GB2312");
        String sid = request.getParameter("sid");


        if (sid == null || sid.length() == 0) {
            fail(request, response, "未填写商品id，请您重新输入");
            return;
        }

        long l_sid = Long.parseLong(sid);
        if(l_sid >  Integer.MAX_VALUE || l_sid < 0){
            fail(request, response, "商品id输入有误");
            //有误下面的都不用做了
            return;
        }

        int int_sid = (int)l_sid;


        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
        }


        //为了可以执行sql语句对字符串类型的数据用单引号包起来
        String str_username = "'" + username + "'";

        String condition = MessageFormat.format(
                "DELETE FROM {0} where sid = {1}",
                tableName, int_sid);

//        DELETE FROM web_used.costume WHERE sid = 2
        Connection con;
        Statement sql;
        ResultSet rs;

        try {
            String uri = "jdbc:mysql://127.0.0.1/" + dataBase + "?" +
                    "user=root&password=root&characterEncoding=gb2312";
            con = DriverManager.getConnection(uri);
            // 判断一下是否唯一

//            String only = "select * from acount where username = '"+username+ "'";
            String only = MessageFormat.format(
                    "select * from {0} " +
                            "where username = {1} and sid = {2}",
                    tableName, str_username, int_sid
            );
            sql=con.createStatement();
            ResultSet only_rs =sql.executeQuery(only);
            boolean m= only_rs.next();
            if(m == false){
                fail(request, response, "在您的名下没有这个商品哦");
            } else {
                try {
                    sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
                    sql.executeUpdate(condition);
                    success(request, response, "删除成功!");
                } catch (SQLException e) {
                    System.out.println(e);
                    fail(request, response, "删除记录失败:" + e.toString());
                }
            }
//            RequestDispatcher dispatcher = request.getRequestDispatcher("/member/add_success.jsp");
//            dispatcher.forward(request, response);

        } catch (SQLException e) {
            System.out.println("删除商品失败");
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void success(HttpServletRequest request, HttpServletResponse response,
                        String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #ea66a6;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>" + backNews + "</h2>");
            out.println("返回");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/shop_module/a_u_d_Servlet.jsp>商品发布页面</a>");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/shop_module/shopList.jsp>商品列表页面</a>");
            out.println("</body></html>");
        } catch (IOException exp) {
        }
    }

    public void fail(HttpServletRequest request, HttpServletResponse response,
                     String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #ea66a6;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>" + backNews + "</h2>");
            out.println("返回");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/shop_module/a_u_d_Servlet.jsp>商品发布页面</a>");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/member/login.jsp>登录页面</a>");
            out.println("</body></html>");
        } catch (IOException exp) {
        }
    }
}